{% extends "base.html" %}

{% block title %}老人信息 - 长者照护计划系统{% endblock %}

{% block page_title %}老人信息{% endblock %}

{% block extra_css %}
<style>
    .filter-section {
        margin-bottom: 20px;
        padding: 15px;
        border: 1px solid #dee2e6;
        border-radius: 5px;
        background-color: #f8f9fa;
    }
    
    .elder-table {
        font-size: 0.9rem;
    }
    
    .elder-table th, .elder-table td {
        padding: 0.5rem;
    }
    
    .elder-row:nth-child(odd) {
        background-color: rgba(0, 0, 0, 0.05);
    }
    
    .elder-row:hover {
        background-color: rgba(0, 123, 255, 0.1);
    }
    
    .json-display {
        background-color: #f8f9fa;
        border: 1px solid #dee2e6;
        border-radius: 4px;
        padding: 10px;
        font-family: monospace;
        white-space: pre-wrap;
        max-height: 300px;
        overflow-y: auto;
    }
    
    #actionsModal .modal-dialog {
        max-width: 90%;
    }
</style>
{% endblock %}

{% block content %}
<div class="row">
    <div class="col-md-12">
        <div class="card">
            <div class="card-header">
                <h5 class="card-title">老人信息</h5>
            </div>
            <div class="card-body">
                <div class="filter-section">
                    <form id="elderFilterForm">
                        <div class="row">
                            <div class="col-md-6">
                                <label for="elder" class="form-label">老人</label>
                                <input type="text" class="form-control" id="elder" name="elder" placeholder="输入老人ID或姓名">
                            </div>
                            <div class="col-md-6">
                                <label class="form-label">&nbsp;</label>
                                <div class="d-grid">
                                    <button type="button" id="filterButton" class="btn btn-primary">筛选</button>
                                </div>
                            </div>
                        </div>
                    </form>
                </div>
                
                <div class="table-responsive">
                    <table class="table table-bordered elder-table">
                        <thead>
                            <tr>
                                <th>ID</th>
                                <th>老人</th>
                                <th>安排状态</th>
                                <th>操作</th>
                            </tr>
                        </thead>
                        <tbody id="elderTableBody">
                            {% for elder in elders %}
                            <tr class="elder-row" data-elder="{{ elder.elder }}">
                                <td>{{ elder.id }}</td>
                                <td>{{ elder.elder }}</td>
                                <td>{{ '已安排' if elder.arrange == '1' else '未安排' }}</td>
                                <td>
                                    <button class="btn btn-sm btn-info view-actions-btn" data-id="{{ elder.id }}" data-actions="{{ elder.actions }}">查看需求</button>
                                    <button class="btn btn-sm btn-success view-schedule-btn" data-id="{{ elder.id }}" data-elder="{{ elder.elder }}">查看排班</button>
                                </td>
                            </tr>
                            {% endfor %}
                        </tbody>
                    </table>
                </div>
                
                <div class="d-flex justify-content-between align-items-center mt-3">
                    <div>
                        <span>显示 <span id="visibleCount">{{ elders|length }}</span> 条记录，共 <span id="totalCount">{{ elders|length }}</span> 条记录</span>
                    </div>
                    <div>
                        <button id="exportButton" class="btn btn-success">
                            <i class="fas fa-file-excel"></i> 导出到Excel
                        </button>
                    </div>
                </div>
            </div>
        </div>
    </div>
</div>

<!-- 老人需求模态框 -->
<div class="modal fade" id="actionsModal" tabindex="-1" aria-labelledby="actionsModalLabel" aria-hidden="true">
    <div class="modal-dialog">
        <div class="modal-content">
            <div class="modal-header">
                <h5 class="modal-title" id="actionsModalLabel">老人需求</h5>
                <button type="button" class="btn-close" data-bs-dismiss="modal" aria-label="Close"></button>
            </div>
            <div class="modal-body">
                <div id="actionsContent" class="json-display"></div>
            </div>
            <div class="modal-footer">
                <button type="button" class="btn btn-secondary" data-bs-dismiss="modal">关闭</button>
            </div>
        </div>
    </div>
</div>
{% endblock %}

{% block extra_js %}
<script>
    $(document).ready(function() {
        // 记录总数
        const totalCount = $('.elder-row').length;
        $('#totalCount').text(totalCount);
        
        // 筛选按钮点击事件
        $('#filterButton').click(function() {
            const elder = $('#elder').val().toLowerCase();
            
            // 筛选表格行
            let visibleCount = 0;
            $('.elder-row').each(function() {
                const rowElder = $(this).data('elder').toString().toLowerCase();
                const rowId = $(this).find('td:eq(0)').text().toLowerCase();
                
                const elderMatch = !elder || rowElder.includes(elder) || rowId.includes(elder);
                
                if (elderMatch) {
                    $(this).show();
                    visibleCount++;
                } else {
                    $(this).hide();
                }
            });
            
            // 更新显示计数
            $('#visibleCount').text(visibleCount);
        });
        
        // 查看需求按钮点击事件
        $(document).on('click', '.view-actions-btn', function() {
            const id = $(this).data('id');
            const actions = $(this).data('actions');
            
            // 显示数据
            try {
                let formattedData = actions;
                if (typeof actions === 'string') {
                    formattedData = JSON.parse(actions);
                }
                $('#actionsContent').text(JSON.stringify(formattedData, null, 2));
            } catch (e) {
                $('#actionsContent').text(actions);
            }
            
            // 显示模态框
            const modal = new bootstrap.Modal(document.getElementById('actionsModal'));
            modal.show();
        });
        
        // 查看排班按钮点击事件
        $(document).on('click', '.view-schedule-btn', function() {
            const id = $(this).data('id');
            const elder = $(this).data('elder');
            
            // 跳转到查看排班页面
            window.open(`/page/view_schedule_page?elder=${id}`, '_blank');
        });
        
        // 导出到Excel按钮点击事件
        $('#exportButton').click(function() {
            // 获取可见行
            const visibleRows = $('.elder-row:visible');
            
            // 如果没有可见行，则提示
            if (visibleRows.length === 0) {
                alert('没有可导出的数据');
                return;
            }
            
            // 创建CSV内容
            let csvContent = 'ID,老人,安排状态\n';
            
            visibleRows.each(function() {
                const id = $(this).find('td:eq(0)').text();
                const elder = $(this).find('td:eq(1)').text();
                const arrange = $(this).find('td:eq(2)').text();
                
                csvContent += `${id},${elder},${arrange}\n`;
            });
            
            // 创建下载链接
            const encodedUri = encodeURI('data:text/csv;charset=utf-8,' + csvContent);
            const link = document.createElement('a');
            link.setAttribute('href', encodedUri);
            link.setAttribute('download', '老人信息.csv');
            document.body.appendChild(link);
            
            // 点击下载链接
            link.click();
            
            // 移除下载链接
            document.body.removeChild(link);
        });
    });
</script>
{% endblock %}
